package cn.edu.buaa.treehole.dao;

import cn.edu.buaa.treehole.dao.exception.DaoException;
import cn.edu.buaa.treehole.dao.exception.PidHasExistException;
import cn.edu.buaa.treehole.dao.exception.PidNotExistException;
import cn.edu.buaa.treehole.pojo.PostInfo;

import java.util.Collection;
import java.util.Map;

/*
 * @author Deng XinYu
 * */
public interface PostDao {
    /*
    * normal_behaviour:
    *   Pre-Condition:
    *       contain(postInfo.getPid()) == false;
    *   Post-Condition:
    *       \suggest PostInfo info = { select * from post_table where PID=postInfo.getPid() };
    *       info != None;
    *       info.equals(postInfo) == true;
    *
    * exception_behaviour:
    *   Pre-Condition:
    *       contain(postInfo.getPid()) == true;
     *   \raise PidHasExistException;
    * */
    void addPost(PostInfo postInfo) throws PidHasExistException, DaoException;

    /*
    * normal_behaviour:
    *   Pre-Condition:
    *       contain(pid) == true;
    *   Post-Condition:
    *       \result == { select * from post_table where PID=pid };
    *
    * exception_behaviour:
    *   Pre-Condition:
    *       contain(pid) == false;
    *   \raise PidNotExistException;
    * */
    PostInfo getPost(long pid) throws PidNotExistException, DaoException;

    /*
     * normal_behaviour:
     *   Pre-Condition:
     *       \old(this).contain(pid) == true;
     *   Post-Condition:
     *       \after(this).contain(pid) == false;
     *
     * exception_behaviour:
     *   Pre-Condition:
     *       \old(this).contain(pid) == false;
     *   \raise PidNotExistException;
     * */
    void deletePost(long id) throws PidNotExistException, DaoException;

    /*
     * normal_behaviour:
     *   Pre-Condition:
     *      { select * from post_table }.size() != 0;
     *   Post-Condition:
     *      \for_all long pid \in { select PID from post_table }
     *          getPost(pid).getDate().comparedTo(getPost(\result).getDate()) <= 0;
     *
     * exception_behaviour:
     *   Pre-Condition:
     *      { select * from post_table }.size() == 0;
     *   \raise PidNotExistException;
     * */
    long getLatestPostPid() throws PidNotExistException, DaoException;

    /*
     * Post-Condition:
     *   \if { select * from post_table where PID=pid } != None, \result == true,
     *   \else, \result == false;
     * */
    boolean contain(long pid) throws DaoException;

    /*
    * Post-Condition:
    *   \for_all long pid \in pidCollection, pid \in \result.keys();
    *   \for_all long pid \in pidCollection,
    *       \if { select * from post_table where PID=pid } != None, \result.get(pid) == true,
    *       \else, \result.get(pid) == false;
    * */
    Map<Long, Boolean> contain(Collection<Long> pidCollection) throws DaoException;

    /*\
    * Pre-Condition:
    *   pidMap will not be used any more after calling;
    *   pidMap.put(Long, Boolean) method is implemented;
    * Post-Condition:
    *   \for_all long pid \in pidMap.keys(), pid \in \result.keys();
    *   \for_all long pid \in pidCollection,
    *       \if { select * from post_table where PID=pid } != None, \result.get(pid) == true,
    *       \else, \result.get(pid) == false;
    * */
    Map<Long, Boolean> contain(Map<Long, Boolean> pidMap) throws DaoException;
}